package com.zy.nft_system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zy.nft_system.domain.Antique;
import com.zy.nft_system.domain.Qntiqueauth;
import com.zy.nft_system.domain.Result;
import com.zy.nft_system.exception.MyException;
import com.zy.nft_system.service.AntiqueService;
import com.zy.nft_system.service.QntiqueauthService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * @author 这是那头猪？
 * @version 1.0
 * @description: TODO
 * @date 2022/12/6 14:31
 */
@RequestMapping("/antique")
@Slf4j
@RestController
public class AntiqueController {

    @Autowired
    AntiqueService antiqueService;

    @Autowired
    QntiqueauthService qntiqueauthService;

    @PostMapping("/insertCollectio")
    public Result insertCollectio(@RequestBody Antique antique){
        log.info("正在添加藏品");
        return antiqueService.insertCollectio(antique);
    }
    @PostMapping("/updataCollectio")
    public Result updataCollectio(@RequestBody Antique antique){
        log.info("正在修改藏品信息");
        return antiqueService.updataCollectio(antique);
    }

    @GetMapping("/deleteCollection")
    @Transactional(rollbackFor = MyException.class,propagation = Propagation.REQUIRED)
    public Result deleteCollection(Integer id){
        log.info("正在删除藏品信息");
        boolean b = antiqueService.removeById(id);
        if (b) {
            log.info("藏品信息删除成功");
            log.info("正在删除藏品权限信息");
            boolean remove = qntiqueauthService.remove(new QueryWrapper<Qntiqueauth>().eq("aid", id));
            if (!remove){
                log.info("未知原因藏品权限信息删除失败");
            }
            log.info("藏品权限信息删除成功");
            return Result.success("删除成功",null);
        }
        log.info("删除失败");
        throw new MyException("未知原因删除失败");
        //return Result.error("未知原因删除失败");
    }

    @GetMapping("/queryCollection")
    public Result queryCollection(String name){
        log.info("正在查询藏品信息");
        return antiqueService.queryCollection(name);
    }
}
